<template>
  <div class="login-container" style="background-color: #f8f8f8;">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >

      <div class="title-container">
        <h3 class="title">Cloud-UI</h3>
      </div>

      <el-form-item :class="focusName === 'user' ? 'light' : ''" prop="loginName">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
        <el-input
          ref="loginName"
          v-model="loginForm.loginName"
          placeholder="账号"
          name="loginName"
          type="text"
          tabindex="1"
          auto-complete="on"
          @focus="focus('user')"
          @blur="blur"
        />
      </el-form-item>

      <el-form-item :class="focusName === 'pwd' ? 'light' : ''" prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"/>
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
          @focus="focus('pwd')"
          @blur="blur"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
                 @click.native.prevent="handleLogin">登录
      </el-button>

    </el-form>

    <div class="copyright">
      <span>Copyright © YZS Inc. All Rights Reserved</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        loginName: 'admin',
        password: 'admin'
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      focusName: '',
      loginRules: {
        loginName: [
          {required: true, trigger: "blur", message: "用户名不能为空"}
        ],
        password: [
          {required: true, trigger: "blur", message: "密码不能为空"}
        ]
      },
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    // 显示隐藏密码
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 登录
    handleLogin() {
      if (!this.validForm("loginForm")) {
        this.$message.error('账户或密码不能为空')
        return
      }
      // 加载开启
      this.loading = true
      // 调用方法
      this.$store.dispatch('user/login', this.loginForm).then(() => {
        // 登录成功跳转到首页
        this.$router.push({path: this.redirect || '/'})
        // 加载关闭
        this.loading = false
      }).catch(err => {
        // 加载关闭
        this.loading = false
      })
    },
    // 获取焦点
    focus(type) {
      this.focusName = type
    },
    // 失去焦点
    blur() {
      this.focusName = ''
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #606266;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #f8f8f8 inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #dcdfe6;
    border-radius: 5px;
    color: #454545;
    transition: all .2s ease-in-out;

    &.light {
      outline: none;
      border: 1px solid #66b1ff;
      box-shadow: 0 0 10px #66b1ff;
    }

    input {
      color: $cursor;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #fff;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  position: relative;
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 480px;
    max-width: 90%;
    padding: 35px 60px 12px;
    margin-top: -50px;
    overflow: hidden;
    box-shadow: 0 0 25px #c2bebe;
    border: 1px solid #eaeaea;
    @media (max-width: 768px) {
      padding: 35px 30px 12px;
    }
    @media (max-width: 414px) {
      padding: 25px 20px 0;
      box-shadow: none;
      border: none;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: #505458;
      font-size: 21px;
      margin: 0px auto 25px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>

<style lang="scss" scoped>
.copyright {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 20px;
  text-align: center;

  span {
    color: #505458;
  }

  a {
    color: #505458;
    margin-left: 5px;
    transition: .35s;

    &:hover {
      color: #0000ee;
    }
  }

  @media (max-width: 991px) {
    bottom: 10px;
    font-size: 12px;
    display: flex;
    flex-direction: column;
    a {
      margin-top: 5px;
    }
  }
}
</style>
